/******************************************************************************/
/*      project Gray                                                          */
/*      Martin Robinson                                                       */
/*                                                                            */
/******************************************************************************/
package regex;

import java.util.regex.Pattern;

/**
 * regular expression for field
 * @author martin
 */
public final class Regex {
  private Regex(){}
  public static final String date = "^[0-9]{4,4}-(0?[1-9])|(1[0-2])-([0-2][0-9])|(3[0-1])$";
  public static final String integer = "^[0-9]+$";
  public static final String money = "^[0-9]+(,[0-9]{1,2})?$";
  public static final String percent = "^[0-9]{1,2}(,[0-9])?$";
  public static final String time = "^([01]?[0-9])|(2[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$";
  
  public static final String name = "^[A-Z][a-z]+([ -][A-Z][a-z]+)*$";
  public static final String adresse = "^[0-9]+, [a-zA-Z ]+$";
  public static final String cp = "^[A-Za-z][0-9][A-Za-z] ?[0-9][A-Za-z][0-9]$";
  public static final String telephone = "^\\([0-9]{3,3}\\) [0-9]{3,3}-[0-9]{4,4}$";
  public static final String url = "^([a-zA-Z0-9]+\\.)+[a-zA-Z]$";
  public static final String email = "^[a-zA-Z0-9]+@([a-zA-Z0-9]+\\.)+[a-zA-Z]$";
  /**
   * validate with given regex
   * @param regex as String
   * @param str as String
   * @return Boolean
   */
  public static Boolean validate(final String regex, final String str){
      return Pattern.matches(regex, str);
  }
  /**
   * validate SQL date
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateDate(final String str){
      return Pattern.matches(date, str);
  }
  /**
   * validate integer
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateInteger(final String str){
      return Pattern.matches(integer, str);
  }
  /**
   * validate money with no more than two digit decimal
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateMoney(final String str){
      return Pattern.matches(money, str);
  }
  /**
   * validate percentage value with no more than two digit decimal
   * @param str as String
   * @return Boolean
   */
  public static Boolean validatePercent(final String str){
      return Pattern.matches(percent, str);
  }
  /**
   * validate Time SQL 
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateTime(final String str){
      return Pattern.matches(time, str);
  }
  /**
   * validate name
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateName(final String str){
    return Pattern.matches(name,str);
  }
  /**
   * validate adresse
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateAdresse(final String str){
    return Pattern.matches(adresse,str);
  }
  /**
   * validate codepostal
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateCP(final String str){
    return Pattern.matches(cp,str);
  }
  /**
   * validate telephone
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateTelephone(final String str){
    return Pattern.matches(telephone,str);
  }
  /**
   * validate url
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateUrl(final String str){
    return Pattern.matches(url,str);
  }
  /**
   * validate email
   * @param str as String
   * @return Boolean
   */
  public static Boolean validateEmail(final String str){
    return Pattern.matches(email,str);
  }
}
